package com.wenwan.manage.mapper;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.wenwan.manage.pojo.BuyReleaseTimes;
import com.wenwan.manage.pojo.IdentityAuthentication;
import com.wenwan.manage.pojo.LoginToken;
import com.wenwan.manage.pojo.VerificationCode;
import com.wenwan.manage.pojo.VerificationCodeFrequency;
import com.wenwan.manage.setting.RedisSetting;
import com.wenwan.manage.setting.SmsSetting;
import com.wenwan.manage.setting.WeiXinSetting;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisDao {

	private final JedisPool jedisPool;

	public RedisDao(String ip, int port) {
		jedisPool = new JedisPool(ip, port);
	}

	public RedisDao() {
		jedisPool = new JedisPool(RedisSetting.HOST, RedisSetting.POIT);
	}

	// 短信验证码 5分钟
	private RuntimeSchema<VerificationCode> schemaVerificationCode = RuntimeSchema.createFrom(VerificationCode.class);
	// 登录token 
	private RuntimeSchema<LoginToken> schemaLoginToken = RuntimeSchema.createFrom(LoginToken.class);
	// 购买发布次数
	private RuntimeSchema<BuyReleaseTimes> schemaBuyReleaseTime = RuntimeSchema.createFrom(BuyReleaseTimes.class);
	// 身份识别
	private RuntimeSchema<IdentityAuthentication> schemaIdentityAuthentication = RuntimeSchema
			.createFrom(IdentityAuthentication.class);
	// 短信验证码频率
	private RuntimeSchema<VerificationCodeFrequency> schemaVerificationCodeFrequency = RuntimeSchema
			.createFrom(VerificationCodeFrequency.class);

	public VerificationCodeFrequency getVerificationCodeFrequency(String id) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "VerificationCodeFrequency:" + id;
				byte[] bytes = jedis.get(key.getBytes());
				if (bytes != null) {
					VerificationCodeFrequency verificationCodeFrequency = schemaVerificationCodeFrequency.newMessage();
					ProtostuffIOUtil.mergeFrom(bytes, verificationCodeFrequency, schemaVerificationCodeFrequency);
					// 过期时间，秒
					verificationCodeFrequency.setTime(jedis.ttl(key));
					return verificationCodeFrequency;
				}
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

	public String putVerificationCodeFrequency(VerificationCodeFrequency verificationCodeFrequency) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "VerificationCodeFrequency:" + verificationCodeFrequency.getId();
				byte[] bytes = ProtostuffIOUtil.toByteArray(verificationCodeFrequency, schemaVerificationCodeFrequency,
						LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
				int timeout = 60;
				String result = jedis.setex(key.getBytes(), timeout, bytes);
				return result;
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

	public IdentityAuthentication getIdentityAuthentication(String id) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "IdentityAuthentication:" + id;
				byte[] bytes = jedis.get(key.getBytes());
				if (bytes != null) {
					IdentityAuthentication identityAuthentication = schemaIdentityAuthentication.newMessage();
					ProtostuffIOUtil.mergeFrom(bytes, identityAuthentication, schemaIdentityAuthentication);
					return identityAuthentication;
				}
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

	public String putIdentityAuthentication(IdentityAuthentication identityAuthentication) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "IdentityAuthentication:" + identityAuthentication.getId();
				byte[] bytes = ProtostuffIOUtil.toByteArray(identityAuthentication, schemaIdentityAuthentication,
						LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
				int timeout = WeiXinSetting.LOGIN_TOKEN_TIME_OUT;
				String result = jedis.setex(key.getBytes(), timeout, bytes);
				return result;
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

	public BuyReleaseTimes getBuyReleaseTimes(String id) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "BuyReleaseTimes:" + id;
				byte[] bytes = jedis.get(key.getBytes());
				if (bytes != null) {
					BuyReleaseTimes buyReleaseTimes = schemaBuyReleaseTime.newMessage();
					ProtostuffIOUtil.mergeFrom(bytes, buyReleaseTimes, schemaBuyReleaseTime);
					return buyReleaseTimes;
				}
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

	public String putBuyReleaseTimes(BuyReleaseTimes buyReleaseTimes) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "BuyReleaseTimes:" + buyReleaseTimes.getId();
				byte[] bytes = ProtostuffIOUtil.toByteArray(buyReleaseTimes, schemaBuyReleaseTime,
						LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
				int timeout = WeiXinSetting.LOGIN_TOKEN_TIME_OUT;
				String result = jedis.setex(key.getBytes(), timeout, bytes);
				return result;
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

	public LoginToken getLoginToken(String id) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "LoginToken:" + id;
				byte[] bytes = jedis.get(key.getBytes());
				if (bytes != null) {
					LoginToken loginToken = schemaLoginToken.newMessage();
					ProtostuffIOUtil.mergeFrom(bytes, loginToken, schemaLoginToken);
					return loginToken;
				}
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

	public String putLoginToken(LoginToken loginToken) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "LoginToken:" + loginToken.getId();
				byte[] bytes = ProtostuffIOUtil.toByteArray(loginToken, schemaLoginToken,
						LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
				int timeout = WeiXinSetting.LOGIN_TOKEN_TIME_OUT;
				String result = jedis.setex(key.getBytes(), timeout, bytes);
				return result;
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

	public VerificationCode getVerificationCode(String id) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "VerificationCode:" + id;
				byte[] bytes = jedis.get(key.getBytes());
				if (bytes != null) {
					VerificationCode verificationCode = schemaVerificationCode.newMessage();
					ProtostuffIOUtil.mergeFrom(bytes, verificationCode, schemaVerificationCode);
					return verificationCode;
				}
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

	public String putVerificationCode(VerificationCode verificationCode) {
		try {
			Jedis jedis = jedisPool.getResource();
			try {
				String key = "VerificationCode:" + verificationCode.getId();
				byte[] bytes = ProtostuffIOUtil.toByteArray(verificationCode, schemaVerificationCode,
						LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
				int timeout = SmsSetting.OUT_TIME_VERIFICATION_CODE;
				String result = jedis.setex(key.getBytes(), timeout, bytes);
				return result;
			} finally {
				jedis.close();
			}
		} catch (Exception e) {

		}
		return null;
	}

}
